package com.example.administrator.commonlibrary.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.support.v4.view.ViewPager;

import com.example.administrator.commonlibrary.bese.BaseAppliction;

/**
 * 作者：wang_hong_wei
 * 创建时间：2018/9/8
 * 描述：
 */
public class ContexUtil {
    /**
     * View获取Activity的工具
     * @param view view
     * @return Activity
     */
    public static Activity getActivity(ViewPager view) {
        Context context = view.getContext();
        while (context instanceof ContextWrapper) {
            if (context instanceof Activity) {
                return (Activity) context;
            }
            context = ((ContextWrapper) context).getBaseContext();
        }
        throw new IllegalStateException("View " + view + " is not attached to an Activity");
    }

    /**
      * @desc 获取Activity 的名字
      * @param
      */
    public static String getActivityName(){
        String  activityName="";
         ActivityManager manager = (ActivityManager) BaseAppliction.getBaseAppliction().getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.RunningTaskInfo info = manager.getRunningTasks(1).get(0);
        String shortClassName = info.topActivity.getShortClassName();    //类名
        String[] split = shortClassName.split("\\.");
        activityName = split[split.length-1];
        return activityName;
    }
    /**
     * @desc 获取Activity 的名字
     * @param context 上下文
     */
    public static String getActivityName(Context context){
        return context.getClass().getSimpleName();
    }
}
